package com.example.demo.service.user;

import com.example.demo.commom.PasswordUtils;
import com.example.demo.commom.TokenUtil;
import com.example.demo.dto.user.login.ULoginDto;
import com.example.demo.dto.user.reg.URegDto;
import com.example.demo.entity.user.Teacher;
import com.example.demo.mapper.user.TeacherMapper;
import com.example.demo.vo.user.LoginVo;
import com.example.demo.vo.user.UserInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:龙宝
 * Date:2023-03-31
 * Time:18:37
 */
@Service
public class TeacherService {
    @Autowired
    private TeacherMapper teacherMapper;
    public Integer reg(URegDto regDto) {
        HashMap<Integer, Object> map = new HashMap<>();
        // 密码加密
        regDto.setPassword(PasswordUtils.encrypt(regDto.getPassword()));
        if(teacherMapper.getTeacherByCardId(regDto.getCard_id()) != null){
            return 0;
        }
        //注册
        return teacherMapper.reg(regDto);
    }
    // 登录
    public LoginVo login(ULoginDto uLoginDto) {
        //校验是否可以进行登录操作
        Teacher teacher = teacherMapper.getTeacherById(uLoginDto.getUser_id());
        if (teacher == null || teacher.getTeacher_id() < 2000 || teacher.getTeacher_id() > 4999) {
            return LoginVo.returnInfo(-1, "教师编号或密码错误");
        }
        if (teacher.getState() > 2 && teacher.getUnfreezing_time() > System.currentTimeMillis()) {
            Long waitTime = (teacher.getUnfreezing_time() - System.currentTimeMillis()) / 1000;
            return LoginVo.returnInfo(-1, "请等待"+waitTime+"s后重试");
        }
        if (teacher.getState() > 2 && teacher.getUnfreezing_time() <= System.currentTimeMillis()) {
            teacher.setState(0);
            teacherMapper.upTeacherState(teacher);
        }
        //密码加盐处理过
        if (PasswordUtils.check(uLoginDto.getPassword(), teacher.getPassword())) {
            teacher.setState(0);
            teacherMapper.upTeacherState(teacher);
            return LoginVo.returnInfo(0, TokenUtil.getToken(teacher.getTeacher_id()));
        } else {
            //1、先将state修改
            teacher.setState(teacher.getState() + 1);
            //2、是否修改取消冻结的最新时间
            if (teacher.getState() > 2) {
                teacher.setUnfreezing_time(System.currentTimeMillis() + 20000);//当前时间往后家100s
            }
            //更新state+unfreezingtime
            teacherMapper.upTeacherState(teacher);
            return LoginVo.returnInfo(-1, "教师编号或密码输入错误");
        }
    }
    // 验证教师身份
    public Boolean authentication(Integer user_id){
        Teacher teacher = teacherMapper.getTeacherById(user_id);
        if (teacher == null || teacher.getTeacher_id() < 2000 || teacher.getTeacher_id() > 4999) {
            return false;
        }
        return true;
    }


    // 查看教师信息
    public List<UserInfoVo> getTeacherInfo(Integer teacher_id){
        List<UserInfoVo> infos = new ArrayList<>();
        if(teacher_id == 0){
            // 查看所有教师信息
            List<Teacher> teachers = teacherMapper.getAllTeacher();
            for(int i = 0;i<teachers.size();i++){
                infos.add(UserInfoVo.createVoByTeacher(teachers.get(i)));
            }
            return infos;
        } else {
            // 查看指定教师信息
            Teacher teacher = teacherMapper.getTeacherById(teacher_id);
            if(teacher == null){
                return null;
            }
            infos.add(UserInfoVo.createVoByTeacher(teacher));
            return infos;
        }
    }

//    // 获取教师信息
//    public HashMap<Integer,Object>  getTeacherById(Integer id) {
//        HashMap<Integer, Object> map = new HashMap<>();
//        //判断用户名是否有人是使用过了
//        Teacher teacher = teacherMapper.getTeacherById(id);
//        if(teacher == null) {
//            map.put(-1,"教师用户不存在");
//            return map;
//        }
//        teacher.setPassword("");
//        map.put(0,teacher);
//        return map;
//    }
//
//    // 修改教师信息
//    public HashMap<Integer,Object>  updateStudentInfo(UserUpdateDto teacher, Integer id) {
//        HashMap<Integer, Object> map = new HashMap<>();
//        Teacher user = teacherMapper.getTeacherById(id);
//        if(user == null || user.getId() <1 ) {
//            map.put(-1,"非法参数");
//            return map;
//        }
//
//        //开始正式修改
//        //密码加盐
//        if(!teacher.getPassword().isEmpty()) {
//            //如果需要修改密码，需要判断旧密码是否正确
//            if(teacher.getOldpassword().isEmpty()){
//                map.put(-1,"参数有误");
//            }
//            if(!PasswordUtils.check(teacher.getOldpassword(),user.getPassword())){
//                map.put(-1,"旧密码有误");
//                return map;
//            }
//            user.setPassword(PasswordUtils.encrypt(teacher.getPassword()));
//        }
//        // 其他信息修改
//        if(StringUtils.hasLength(teacher.getEmail())){
//            user.setEmail(teacher.getEmail());
//        }
//        if(StringUtils.hasLength(teacher.getPhone())){
//            user.setPhone(teacher.getPhone());
//        }
//        teacherMapper.updateTeacherInfo(user);
//        map.put(0, "修改成功");
//        return map;
//    }
}
